﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace FOBVT.Web.FOBVT2012.en
{
	public partial class ApplyBack : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void BtnSubmit_Click(object sender, EventArgs e)
		{
			Submit("insert");
		}

		private void Submit(string type)
		{
			System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
			FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();
			ArrayList sql = new ArrayList();
			sql.Clear();

			string organization = txtOrganization.Text.Trim();
			string organizationCategory =""; //= ddlCategory.SelectedValue.Trim();

			for (int i = 0; i < this.ddlCategory.Items.Count; i++)
			{
				if (this.ddlCategory.Items[i].Selected)
				{
					organizationCategory += ddlCategory.Items[i].Text + ",";
				}
			}
			organizationCategory += txtCategoryOther.Text.Trim();
			//string organizationCategoryOther = txtCategoryOther.Text.Trim();
			string organizationDescription = txtDescription.Text.Trim();
			string name = txtName.Text.Trim();
			string title = txtTitle.Text.Trim();
			string company = txtCompany.Text.Trim();
			string address = txtAddress.Text.Trim();
			string signature = txtSignature.Text.Trim();
			string signatrueTitle = txtSignatureTitle.Text.Trim();
			string phone = txtPhone.Text.Trim();
			string facsimile = txtFacsimile.Text.Trim();
			string email = txtEmail.Text.Trim();
			string website = txtWebsite.Text.Trim();
			string data = TxtData.Text.Trim();
			string date = System.DateTime.Now.ToString();

			
			int varCustomerID = 0;
			string strCustomerID = "";
			if ((this.hdModifyID.Value.Length > 0) || (type == "Modify"))
			{
				varCustomerID = Convert.ToInt32(this.hdModifyID.Value.ToString());
				sql.Add("UPDATE  ApplyMember SET Organization='" + organization +
					"',OrganizationCategory='" + organizationCategory +
					"',OrganizationDescription='" + organizationDescription +
					"',Name='" + name +
					"',Title='" + title +
					"',Company='" + company +
					"',Address='" + address +
					"',Signature='" + signature +
					"',SignatureTitle=" + signatrueTitle +
					"',Phone='" + phone +
					"',Facsimile='" + facsimile +
					"',Email='" + email +
					"',Website='" + website +
                    "',data='" + data +
					" WHERE (ApplyMemberID ='" + this.hdModifyID.Value + "')");
			}
			else
			{
				varCustomerID = DBClassTmp.GetIDInfo("SELECT max(CLng(Right(ApplyMemberID,len(ApplyMemberID)-Instr(1,ApplyMemberID,'V',1)))) FROM ApplyMember", Conn) + 1;
				strCustomerID = "2012FOBTV" + varCustomerID.ToString();
				if (!CheckUserInfoExists())
				{
					sql.Add("INSERT INTO ApplyMember(ApplyMemberID,Organization,OrganizationCategory,OrganizationDescription,Name,Title,Company,Address,Signature,SignatureTitle,Phone,Facsimile,Email,Website,data,ApplyDate)VALUES ('" +
						strCustomerID + "','" +
						organization + "','" +
						organizationCategory + "','" +
						organizationDescription + "','" +
						name + "','" +
						title + "','" +
						company + "','" +
						address + "','" +
						signature + "','" +
						signatrueTitle + "','" +
						phone + "','" +
						facsimile + "','" +
						email + "','" +
						website + "','" +
						data + "','" +
						System.DateTime.Now.ToString() + "')");
				}
				else
				{
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "This user exists!");
					return;
				}

			}
			if (DBClassTmp.ExcuteSQL(sql, Conn))
			{
				if ((this.hdModifyID.Value.Length > 0) || (type == "Modify"))
				{
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Modify successfully!!");
				}
				else
				{
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Add successfully!!");
				}

				//Response.Redirect("RegistrationFee_en.aspx");
			}
			else
			{
				FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Operation Failed!!");
			}
		}

		/// <summary>
		/// 注册时，检查用户的Email是否存在
		/// </summary>
		/// <returns></returns>
		public bool CheckUserInfoExists()
		{
			string strSQL = "SELECT count(*) FROM ApplyMember WHERE (Email='" + txtEmail.Text.Trim() + "')";
			System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
			DataSet ds = new DataSet();
			MainClass.DBClass DBClassTmp = new MainClass.DBClass();
			ds = DBClassTmp.GetDataSet(strSQL, Conn);
			if (Convert.ToInt32(ds.Tables[0].Rows[0][0]) == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

	}
}